package org.jeecg.modules.deal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.deal.entity.DesReportTable;
import org.jeecg.modules.deal.entity.StatisticsTable;
import org.jeecg.modules.deal.mapper.DesReportTableMapper;
import org.jeecg.modules.deal.service.DesReportTableService;
import org.jeecg.modules.meta.entity.SysTableinfo;
import org.jeecg.modules.meta.service.ISysTableinfoService;
import org.jeecg.modules.run.entity.RunDetail;
import org.jeecg.modules.run.service.IRunDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 订阅查询
 * @Author: jeecg-boot
 * @Date: 2019-07-08
 * @Version: V1.0
 */
@Service
public class DesReportTableServiceServiceImpl extends ServiceImpl<DesReportTableMapper, DesReportTable> implements DesReportTableService {

    @Autowired
    DesReportTableMapper desReportTableMapper;

    @Autowired
    ISysTableinfoService iSysTableinfoService;


    @Override
    public IPage<DesReportTable> queryStrategyHasSSc(Page<DesReportTable> page, String fdTableName, String fdSchema, String fdProNum) {
        List<String> tableId = null;
        if (StringUtils.isNotEmpty(fdTableName)) {
            tableId = getTabIdListByName(fdTableName);
            //如果表名称查不到表返回空
            if (null == tableId || tableId.size() == 0) {
                return new Page<>();
            }
        }

        return convertDesReportTable(desReportTableMapper.queryStrategyHasSSc(page, tableId, fdSchema, fdProNum));
    }

    @Override
    public IPage<DesReportTable> queryRunDetail(Page<DesReportTable> page, String fdTableName, String runDate_begin, String runDate_end,String fdTableinfoType) {
        List<String> tableId = null;
        if (StringUtils.isNotEmpty(fdTableName)) {
            tableId = getTabIdListByName(fdTableName);
            //如果表名称查不到表返回空
            if (null == tableId || tableId.size() == 0) {
                return new Page<>();
            }
        }

        if (StringUtils.isEmpty(runDate_begin) && StringUtils.isNotEmpty(runDate_end)) {
            runDate_begin = runDate_end.substring(0, 10) + " 00:00:00";
        } else if (StringUtils.isEmpty(runDate_end) && StringUtils.isNotEmpty(runDate_begin)) {
            runDate_end = runDate_begin.substring(0, 10) + " 23:59:59";
        }

        return convertDesReportTable(desReportTableMapper.queryRunDetail(page, tableId, runDate_begin, runDate_end, fdTableinfoType));
    }

    private IPage<DesReportTable> convertDesReportTable(IPage<DesReportTable> desReportTableIPage) {
        List<DesReportTable> desReportTables = desReportTableIPage.getRecords();
        if (null == desReportTables) {
            return desReportTableIPage;
        }

        for (DesReportTable desReportTable : desReportTables) {
            if (desReportTable.getFdType().equals("1")) {
                desReportTable.setFdType("自动");
            } else if (desReportTable.getFdType().equals("2")) {
                desReportTable.setFdType("手动");
            }
        }

        return desReportTableIPage;
    }

    private List<String> getTabIdListByName(String tableName) {
        QueryWrapper<SysTableinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("fd_tablename", tableName);
        List<SysTableinfo> list = iSysTableinfoService.list(queryWrapper);
        if (null == list || list.size() == 0) {
            return new ArrayList<>();
        }

        return covTableInfoString(list);
    }


    private List<String> covTableInfoString(List<SysTableinfo> list) {
        List<String> strings = new ArrayList<>();
        for (SysTableinfo sysTableinfo : list) {
            strings.add(sysTableinfo.getFdTableid());
        }
        return strings;
    }

    //策略查询
    @Override
    public IPage<DesReportTable> queryStrategyTable(Page<DesReportTable> page, String fdTableName, String fdSchema) {
        List<String> tableId = null;
        if (StringUtils.isNotEmpty(fdTableName)) {
            tableId = getTabIdListByName(fdTableName);
            //如果表名称查不到表返回空
            if (null == tableId || tableId.size() == 0) {
                return new Page<>();
            }
        }
        return convertDesReportTable2(desReportTableMapper.queryStrategyTable(page, tableId, fdSchema));
    }


    private IPage<DesReportTable> convertDesReportTable2(IPage<DesReportTable> desReportTableIPage) {
        List<DesReportTable> desReportTables = desReportTableIPage.getRecords();
        if (null == desReportTables) {
            return desReportTableIPage;
        }

        for (DesReportTable desReportTable : desReportTables) {
            if (desReportTable.getAutoSscId() != null) {
                desReportTable.setAutoSscId("已订阅");

            } else {
                desReportTable.setAutoSscId("未订阅");
            }
            if (desReportTable.getSgSscId() != null) {
                desReportTable.setSgSscId("已订阅");

            } else {
                desReportTable.setSgSscId("未订阅");
            }
        }
        return desReportTableIPage;
    }

    //统计报表
    @Override
    public IPage<StatisticsTable> queryStatisticsTable(Page<StatisticsTable> page) {

        return convertDesReportTable3(desReportTableMapper.queryStatisticsTable(page)) ;
    }

    private IPage<StatisticsTable> convertDesReportTable3(IPage<StatisticsTable> StatisticsTableIPage) {
        List<StatisticsTable> statisticsTable =StatisticsTableIPage.getRecords();
        if (null == statisticsTable) {
            return StatisticsTableIPage;
        }
        //创建数组
        List<String> table = new ArrayList();
        for (StatisticsTable list : statisticsTable) {
            table.add(list.getFdSchema());
        }
        List<StatisticsTable> subscription = desReportTableMapper.queryStatisticsSubscription(table);
        List<StatisticsTable> allocationSubscription = desReportTableMapper.queryAllocationSubscription(table);

        for (StatisticsTable statisticstable : statisticsTable) {
            statisticstable.setDscNum(0);
            statisticstable.setResNum(statisticstable.getAllNum());
            statisticstable.setAlrNum(0);
            statisticstable.setSurNum(statisticstable.getAllNum());
            //循环已订阅
            if (subscription != null) {
                for (StatisticsTable subscr : subscription) {
                    if (statisticstable.getFdSchema().equals(subscr.getFdSchema())) {
                        //设置表中已订阅和未订阅
                        statisticstable.setDscNum(subscr.getDscNum());
                        statisticstable.setResNum(statisticstable.getAllNum() - subscr.getDscNum());
                        break;
                    }

                }
            }
            //循环已配置
            if (allocationSubscription != null) {
                for (StatisticsTable allcation : allocationSubscription) {
                    if (statisticstable.getFdSchema().equals(allcation.getFdSchema())) {
                        //设置表中已配置和未配置
                        statisticstable.setAlrNum(allcation.getAlrNum());
                        statisticstable.setSurNum(statisticstable.getAllNum() - allcation.getAlrNum());
                        break;
                    }

                }
            }
        }



        return StatisticsTableIPage;
    }

}
